import Vue from 'vue'
import VueRouter from 'vue-router'
import login from "../views/Login"
import layout from "../layout/index.vue"
//引入二级路由
import Page from "./page"
Vue.use(VueRouter)
//缺省的页面
import err from "./err"
const routes = [
  ...err,
  {
    path:"/login",
    name:"/login",
    component:login//这是一级路由
  },
  {
  path:'/',
  name:'/layout',
  component:layout,
  //layout页面下的Page页面
  children:Page
 
    
  

  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to, from, next) => {
  //获取token值
  const token = localStorage.getItem("token")
  //如果token值不正确并且登录页面的路径不对的话，
  //就还在登录页面
  //写token,必须在页面加token,要不然不管啥情况他都不会跳转页面，即使账号密码正确，它也跳不过去
  if (!token && to.path !== "/login"&& !to.meta.filter ) {
    next("/login")
  } else {
    next()
  }
})

export default router
